//
//  ExtraPeriodViewController.m
//  TourDeGarde
//
//  Created by Guillaume SOURDRILLE on 03/05/12.
//  Copyright (c) 2012 Astek. All rights reserved.
//

#import "ExtraPeriodViewController.h"
#import "CustodyViewController.h"
#import "ExtraPeriodCell.h"

@implementation ExtraPeriodViewController
@synthesize dataLabel;
@synthesize titleLabel;
@synthesize noDataLabel;
@synthesize backButton;

@synthesize extraPeriodTable,listCutodies,messageUtils,addButton,dateUtils;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
         self.tabBarItem = [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"Commun.button.extra",@"") image:[UIImage imageNamed:@"extra.png"] tag:0];
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    //To have a background
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    UITableView *tv = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    [self.view addSubview:tv];
    [self.view sendSubviewToBack:tv];

    [self initLabel];
    
}

- (void)viewDidUnload
{

    [self setExtraPeriodTable:nil];
    [self setAddButton:nil];
    [self setBackButton:nil];
    [self setTitleLabel:nil];
    [self setNoDataLabel:nil];
    [self setDataLabel:nil];
    [super viewDidUnload];
    listCutodies = nil;
    extraPeriodTable = nil;
    messageUtils = nil;
    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [listCutodies count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"cellForRowAtIndexPath");
    
    NSString *uniqueIdentifier = @"extraCell";
    
    ExtraPeriodCell  *cell = nil;
    
    cell = (ExtraPeriodCell *) [self.extraPeriodTable dequeueReusableCellWithIdentifier:uniqueIdentifier];
    
    
    Custody *custody = [self.listCutodies objectAtIndex:[indexPath row]];
    
    
    if(!cell)
    {
        
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ExtraPeriodCell" owner:nil options:nil];
        
        for(id currentObject in topLevelObjects)
        {
            if([currentObject isKindOfClass:[ExtraPeriodCell class]])
            {
                cell = (ExtraPeriodCell *)currentObject;
                break;
            }
        }
    }
    
    
    cell.nameLabel.text = [custody name];
    cell.beginDateLabel.text = [dateUtils getStringFromDate:[custody beginDate]];
    cell.endDateLabel.text = [dateUtils getStringFromDate:[custody endDate]];
    cell.fromLabel.text = NSLocalizedString(@"Composant.cell.from", @"");
    cell.toLabel.text = NSLocalizedString(@"Composant.cell.to", @"");
    if ([custody isActive]){
        cell.isCustodyLabel.text = NSLocalizedString(@"Composant.cell.isCustody", @"");
    }else{
        cell.isCustodyLabel.text = NSLocalizedString(@"Composant.cell.isNotCustody", @"");
    }
    
    [cell hideDetails];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	// open a alert with an OK and cancel button
	Custody *custody = [listCutodies objectAtIndex:[indexPath row]];
    CustodyViewController *custodyViewController = [[CustodyViewController alloc]initWithCustody:custody];
    [self presentModalViewController:custodyViewController animated:YES];

}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return YES if you want the specified item to be editable.
    return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [listCutodies removeObjectAtIndex:[indexPath row]];
        [messageUtils setArrayForLabel:@"listCustodies" value:listCutodies];
        [self initData];
    }    
}
- (IBAction)addCustody:(id)sender {
   
    CustodyViewController *custodyViewController = [[CustodyViewController alloc]init];
    [self presentModalViewController:custodyViewController animated:YES];
    
}

- (IBAction)processBack:(id)sender {
    [self dismissModalViewControllerAnimated:YES];
}

- (void)initData {
    listCutodies = [messageUtils getArray:@"listCustodies"];
    NSSortDescriptor *sortDescriptor;
    sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"beginDate"
                                                 ascending:YES] ;
    NSMutableArray *sortDescriptors = [NSMutableArray arrayWithObject:sortDescriptor];
    listCutodies = [NSMutableArray arrayWithArray:[listCutodies sortedArrayUsingDescriptors:sortDescriptors]];
    if (listCutodies.count == 0){
        noDataLabel.text = NSLocalizedString(@"Extra.nodata", @"");
        noDataLabel.hidden = NO;
        dataLabel.hidden = YES;
        
    }else{
        noDataLabel.hidden = YES;
        dataLabel.hidden = NO;
        dataLabel.text = NSLocalizedString(@"Extra.data",@""); 
    }
    [self.extraPeriodTable reloadData];
}

- (void)viewWillAppear:(BOOL)animated {
    [self initData];
}

- (void)initLabel{
    addButton.title = NSLocalizedString(@"Commun.button.add", @"");
    backButton.title = NSLocalizedString(@"Commun.button.back", @"");
    messageUtils = [[MessageUtils alloc]init];
    dateUtils = [[DateUtils alloc]init];
    [titleLabel setText:NSLocalizedString(@"Extra.title", @"")];
}
@end
